package com.database.objects;

import java.math.BigDecimal;

public class Priority {

	public static final BigDecimal MAX_VALUE = new BigDecimal(5);

	public static final BigDecimal HALF_VALUE = new BigDecimal(3);

	public static final BigDecimal MIN_VALUE = new BigDecimal(1);

	public static Integer scaleAccordingDistribution(Distribution distribution, String value) {
		if (distribution.isGoogleCode()) {
			return scaleGoogleCode(value);
		} else if (distribution.isSourceForge()) {
			return scaleSourceForge(value);
		}
		return MIN_VALUE.intValue();
	}

	private static Integer scaleGoogleCode(String value) {
		return scale(value);
	}

	private static Integer scaleSourceForge(String value) {
		return scale(Integer.valueOf(value), 9);
	}

	private static Integer scale(Integer value, Integer maxValue) {
		BigDecimal valueDecimal = new BigDecimal(value);
		BigDecimal maxValueDecimal = new BigDecimal(maxValue);
		BigDecimal result = valueDecimal.multiply(MAX_VALUE).divide(maxValueDecimal, 0, BigDecimal.ROUND_HALF_UP);
		return result.intValue();
	}

	private static Integer scale(String value) {
		BigDecimal result = MIN_VALUE;
		value = processValue(value);
		if (value.equals("HIGH")) {
			result = MAX_VALUE;
		} else if (value.equals("MEDIUM")) {
			result = HALF_VALUE;
		} else if (value.equals("LOW")) {
			result = MIN_VALUE;
		}
		return result.intValue();
	}

	public static String processValue(String value) {
		return value.trim().replace(" ", "").toUpperCase();
	}

}